package com.tbvtc.agriculturalcreditchainserver2025.comment.sms;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.teaopenapi.models.Config;
import com.tbvtc.agriculturalcreditchainserver2025.properties.AliyunSMSProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration // 配置类
@EnableConfigurationProperties(AliyunSMSProperties.class)
@ConditionalOnProperty(name = "aliyun.sms.endpoint")
public class AliyunSMSConfiguration {

    @Autowired
    @Qualifier("aliyunSMSProperties")
    private AliyunSMSProperties properties;

    @Bean
    public Client smsClient() {
        Config config = new Config(); // 配置
        config.setAccessKeyId(properties.getAccessKeyId()); // 设置密钥
        config.setAccessKeySecret(properties.getAccessKeySecret()); // 设置密钥
        config.setEndpoint(properties.getEndpoint()); // 设置域名
        try {
            return new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }
}